import { Component, OnInit } from '@angular/core';
import {AppConstants, Components, MsgConstants, PermissionType} from "../../../constants/constants";
import {MsgResult} from "../../../models/msg-result";
import {Observable} from "rxjs/Observable";
import {PortalUtilService} from "../../portal-util.service";
import {ResponseUtil} from "../../../util/response-util";
import {TreeUtil} from "../../../util/tree-util";
import {Menu} from "../../../models/menu";
import {PortalsService} from "../../portals.service";
import {ActivatedRoute, Router} from "@angular/router";
import {AuthService} from "../../../services/auth.service";
import {DataStorageUtil} from "../../../util/data-storage-util";
import {OnlineService} from "../../../services/online.service";
import {OneDialogService} from "../../../widgets/one-dialog/one-dialog.service";
import {Dialog} from "../../../widgets/one-dialog/dialog";

declare var $;

@Component({
  selector: 'app-big-screen-header',
  templateUrl: './big-screen-header.component.html',
  styleUrls: ['./big-screen-header.component.css']
})
export class BigScreenHeaderComponent implements OnInit {

  menus : Menu[];
  isShowSetting: boolean = false;
  isDashBoard: boolean = false;
  activeBtn: string = 'dash';
  myName:string;

  constructor(
    private portalsService : PortalsService,
    private portalUtilService: PortalUtilService,
    private responseUtil : ResponseUtil,
    private treeUtil : TreeUtil,
    private router: Router,
    private route: ActivatedRoute,
    private authService: AuthService,
    private dataStorage:DataStorageUtil,
    private onlineService : OnlineService,
    private dialogService: OneDialogService
  ) { }

  ngOnInit() {
    this.getMenus();
    this.getName();
    this.initAdminLte();
  }
  initAdminLte() {
    if (!$.AdminLTE) {
      this.portalUtilService.initAdminLte();
    }
  }
  getName(){
    this.onlineService.getAccountName()
      .subscribe(
        result => this.responseUtil.success(result,this.extractAccountName),
        error => this.responseUtil.error(error)
      );
  }
  logout() {
    //退出信息modal框
    let dialog = new Dialog();
    dialog.type = Dialog.DialogType.Info;
    dialog.isModal = true;
    dialog.title = "退出信息";
    dialog.content = "确定退出系统吗？";
    dialog.cancelBtn = "取消";
    dialog.okBtn = "确定";
    dialog.showCancelBtn=true;
    dialog.okBtnFun = this.requestLogout;
    this.dialogService.push(dialog);
  }

  /**
   * 确认退出时，回调的请求退出方法
   */
  private requestLogout = () => {
    this.authService.logout().subscribe(
      result => this.responseUtil.success(result, this.processResult),
      error => this.responseUtil.error(error)
    );
  }

  /**
   * 成功时，跳转到登录页面
   * @param result
   */
  private processResult = (result: MsgResult) => {
    if (result.code == MsgConstants.MSG_CODE_OK) {
      this.dataStorage.clear();
      this.router.navigateByUrl(Components.authenComponent);
    }else{
      this.dialogService.info(result.message,()=> {
        this.router.navigateByUrl(Components.authenComponent);
      },()=> {
        this.router.navigateByUrl(Components.authenComponent);
      });
    }
  }

  /**
   *跳转到个人中心页面
   * @param result
   */
  jumpMe() {
    this.router.navigate(['me'],{ relativeTo: this.route});
  }


  /**
   * 提取账户名
   * @param result
   */
  private extractAccountName = (result : MsgResult)=> {
    if (result && result.code == MsgConstants.MSG_CODE_OK) {
      this.myName = result.data;
    }
    if (result && result.code != MsgConstants.MSG_CODE_OK) {
      this.dialogService.info(result.message);
    }
  }
  private getMenus(){
    this.getMenusByConfig().subscribe(
      result => this.responseUtil.success(result,this.process),
      error => this.responseUtil.error(error)
    );
  }

  private getMenusByConfig():Observable<any>{
    if(AppConstants.sysId){
      return this.portalsService.getChildMenus(AppConstants.sysId);
    }else{
      return this.portalsService.getPermissions(PermissionType.menus);
    }
  }

  private process = (result : MsgResult) => {
    let rootId = AppConstants.sysId ? AppConstants.sysId : null;
    let rootNode = {id:rootId};
    let ownedMneus:any= this.treeUtil.filterNodes(rootNode,result.data);
    if(ownedMneus.length == 0){
      this.menus = [];
    }else{
      this.menus = ownedMneus[0].children;
    }
  }

  dropDownMenuToggle(){
    let sidebarMenu = $('.sidebar');
    if ( sidebarMenu.css( 'display' ) == 'none' ) {
      sidebarMenu.show();
    } else {
      sidebarMenu.hide();
    }
  }
  dropDownMenuHide(){
    $('.sidebar').hide();
  }
  dropDownSettingToggle(){
    if ( this.isShowSetting ) {
      this.isShowSetting = false;
    } else {
      this.isShowSetting = true;
    }
  }
  jumpToDv(){
    this.router.navigate(['./'], {relativeTo: this.route});
  }
  jumpDashboard(url){
    if(url != './'){
      this.activeBtn = url;
    }else{
      this.activeBtn = 'dash';
    }

    this.router.navigate([url], {relativeTo: this.route});
  }

}
